# Based on procedure described in Mordin on Time.

# Use official gradings to bring various times into line, so that they would all
# (roughly) have been achievable by horse of similar class. To do this, deduct 
# 2.50 seconds per mile from races in slowest class and linearly-proportionate 
# numbers of seconds from races between slowest and fastest classes
races <- transform(races, winning_time_adjstd =
  winning_time_secs - (class - 1) * 2.5 / 6.0 / distance_yards * 1760)

# To estimate standard time for course and distance, calculate median of
# adjusted times
stdtimes <- aggregate(winning_time_adjstd ~ course + distance_yards,
                      races, FUN = median)

races <- merge(races, stdtimes,
               by = c("course", "distance_yards"), suffixes = c("", ".median"))
names(races)[names(races) == "winning_time_adjstd.median"] <- "stdtime"
rm(stdtimes)